/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#pragma once

#include "结构数据.h"



typedef struct {
	uint64 m_块类型;
	uint64 m_版本号;
	//int8   m_Name[16];
	uint64 m_字节数;
	uint8* m_data;
}S_文件块;

Inline S_文件块 f_file_构建文件块信息(uint64 type, uint64 版本号) {
	S_文件块 文件块 = { type, 版本号, 0, nullptr };
	return 文件块;
}

Inline void f_file_保存文件块(FILE* f, S_文件块& 文件块, S_UI8Array* 块 = nullptr) {
	if (块) {
		文件块.m_data = 块->ptr_userData;
		文件块.m_字节数 = 块->count;
	}

	fwrite(&文件块, sizeof(S_文件块), 1, f);
	fwrite(文件块.m_data, 文件块.m_字节数, 1, f);

	if (块) f_core_array_free((S_Array*)块);
}

Inline S_文件块 f_file_读取文件块(FILE* f) {
	S_文件块 文件块;
	fread(&文件块, sizeof(S_文件块), 1, f);
	文件块.m_data = (uint8*)calloc(文件块.m_字节数, 1);
	fread(文件块.m_data, 文件块.m_字节数, 1, f);
	return 文件块;
}


Inline bool f_file_版本对比(S_文件块& 块, uint64 底版本号, uint64 高版本号) {
	if (块.m_版本号 > 高版本号 || 块.m_版本号 < 底版本号) {
		return false;
	}
	return true;
}




//void f_file_包文件块(const uint8* data, uint64 dataLen, S_UI8Array* 块) {
//	f_core_array_push_back_block((S_Array*)块, (uint8*)&(((S_RealArray*)array)->count), sizeof(uint32));
//}
//
//void f_file_解文件块(const uint8* data, uint64 dataLen, S_UI8Array* 块) {
//	
//}





Inline uint8* f_file_读文件块(FILE* f, S_文件块& 块) {
	fread(&块, sizeof(S_文件块), 1, f);
	uint8* data = (uint8*)calloc(块.m_字节数, 1);
	fread(data, sizeof(uint8), 块.m_字节数, f);
	return data;
}

Inline void f_file_写文件块(FILE* f, const S_文件块& 块, const uint8* data) {
	fwrite(&块, sizeof(S_文件块), 1, f);
	fwrite(data, sizeof(uint8), 块.m_字节数, f);
}



